//
//  AnuDetailViewController.m
//  TinyFortune
//
//  Created by lc on 16/6/11.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "AnuDetailViewController.h"

@interface AnuDetailViewController ()<UITextViewDelegate>{
    UIBarButtonItem *_rightBar;
    NSString *_timeString;
}

@end

@implementation AnuDetailViewController
#pragma mark - createUI
- (void)createUI{
    //实例化导航条
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(onClicked)];
    [self.navigationItem setLeftBarButtonItem:leftBar];
    self.navigationController.navigationBar.translucent = NO ;
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:86/255.0f green:202/255.0f blue:255/255.0f alpha:0.5]];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self setTitle:@"日记本"];
    _rightBar = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(complete)];
    [self.navigationItem setRightBarButtonItem:_rightBar];
    //添加滑动手势
    UISwipeGestureRecognizer *rightSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightGo)];
    [self.view addGestureRecognizer:rightSwip];
    [rightSwip setDirection:UISwipeGestureRecognizerDirectionRight];
    
    
    
    //赋值前做准备
    [self.navigationItem.rightBarButtonItem.customView setHidden:NO];
    NSDate *date = [NSDate new];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy年MM月dd日 HH:mm";
    _timeString = [formatter stringFromDate:date];
    
    //实例化textView
    _textView.delegate = self ;
    if (!_isFirst) {
        [_textView becomeFirstResponder];
        
        
    }
    
    if (_dic) {
        self.dayLabel.text = _dic[@"time"];
        self.textView.text = _dic[@"content"];
        //模仿ios日记本的功能,没改就隐藏完成
        self.navigationItem.rightBarButtonItem = nil;
    }else{
        //实例化标题
        
        self.dayLabel.text = _timeString;
        
    }
    
}
#pragma mark - textView协议
- (void)textViewDidChange:(UITextView *)textView{
    [self.navigationItem setRightBarButtonItem:_rightBar];
}
#pragma mark - 添加左滑手势
- (void)rightGo{
    [self onClicked];
}

- (void)onClicked{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)complete{
    //如果是新的日记就保存,不是新的日记（不显示完成按钮）如果修改了就从服务器上修改内容
    AVQuery *queryTime = [AVQuery queryWithClassName:@"diary"];
    
#pragma mark - 考虑到同一时间可能会有不同的用户上传日记,查询的条件不能单单只有时间,还必须有用户表单的数据,所有需要多表联查
    [queryTime whereKey:@"time" equalTo:self.dayLabel.text];
    AVQuery *nameQuery =[AVQuery queryWithClassName:@"diary"];
    [nameQuery whereKey:@"user" equalTo:[AVUser currentUser].mobilePhoneNumber];
    AVQuery *loverQuery = [AVQuery queryWithClassName:@"diary"];
    [loverQuery whereKey:@"user" equalTo:[[AVUser currentUser]objectForKey:@"lovers"]] ;
    AVQuery *query = [AVQuery orQueryWithSubqueries:@[nameQuery,loverQuery]];
    AVQuery *endQuery = [AVQuery andQueryWithSubqueries:@[query,queryTime]];
    
    
    
    
    
    [endQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count != 0) {
            AVObject *obj = (AVObject *)objects.firstObject;
            if (![[obj objectForKey:@"content"] isEqualToString:self.textView.text]) {
                NSLog(@"22   %@",[obj objectForKey:@"time"]);
                //修改之后的日记
                [obj setObject:_timeString forKey:@"time"];
                [obj setObject:self.textView.text forKey:@"content"];
#pragma mark 我在这里保留了能更改对象日记本的权限，因为我觉得，两个情侣在日记本打情骂俏也挺好玩的，哈哈
                //[obj setObject:[AVUser currentUser].mobilePhoneNumber forKey:@"user"];
                [obj saveInBackground];
            }
            
        } else {
            NSLog(@"11");
            //新的日记
            AVObject *object = [[AVObject alloc]initWithClassName:@"diary"];
            [object setObject:self.dayLabel.text forKey:@"time"];
            [object setObject:self.textView.text forKey:@"content"];
            [object setObject:[AVUser currentUser].mobilePhoneNumber forKey:@"user"];
            [object saveInBackground];
        }

    }];
    
      [self onClicked];
}
#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
